//
//  KAChangeMobileVC.swift
//  kano
//
//  Created by Frank on 2024/10/31.
//

import Foundation
import JXSegmentedView

class KAChangeMobileVC:KABaseVC {
    
    //JXSegmentedBaseDataSource
    lazy var segmentedDataSource: JXSegmentedTitleDataSource = {
        let dataSource = JXSegmentedTitleDataSource()
        dataSource.isTitleColorGradientEnabled = true
        dataSource.titles = ["1", "2", "3", "4"]
        dataSource.titleNormalColor = .black
        dataSource.titleSelectedColor = .themeColor
        return dataSource
    }()
    lazy var segmentedView: JXSegmentedView = {
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorWidth = 20
        indicator.indicatorColor = .themeColor
        let segmentedView = JXSegmentedView()
        segmentedView.indicators = [indicator]
        segmentedView.dataSource = segmentedDataSource
        segmentedView.delegate = self
        segmentedView.frame = CGRect(x:0, y: UIDevice.ka_fullNavHeight, width: view.bounds.width, height: kscale(50))
        segmentedView.backgroundColor = .white
        segmentedView.ka_addTopCorner(withRadius: kscale(16))
        segmentedView.isUserInteractionEnabled = false
        return segmentedView
    }()
    lazy var listContainerView: JXSegmentedListContainerView! = {
        let container = JXSegmentedListContainerView(dataSource: self)
        container.scrollView.isScrollEnabled = false
        container.frame = CGRectMake(0, segmentedView.bottom, var_screenWidth, UIDevice.ka_viewHeight-segmentedView.height)
        return container
    }()
    
    var model: KABillDetailModel?
    var currentIndex: Int = 0
    var childVcs: [JXSegmentedListContainerViewListDelegate] = []
    var oldMobileVC: KAOldMobileVerVC = {
        let oldMobileVC = KAOldMobileVerVC()
        return oldMobileVC
    }()
    var userInfoVC: KAUserInfoVerVC = {
        let userInfoVC = KAUserInfoVerVC()
        return userInfoVC
    }()
    
    var newMobileVC: KANewMobileVerVC = {
        let newMobileVC = KANewMobileVerVC()
        return newMobileVC
    }()
    
    var newPwdVC: KANewMobilePwdVC = {
        let newPwdVC = KANewMobilePwdVC()
        return newPwdVC
    }()
    
    init() {
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .colorf2f2f5
        
        ka_showCustomerNavBar(type: .normal)
        var_navBar.var_titleLabel.text = "Change your sign-in mobile phone"
        
        view.addSubview(segmentedView)
        segmentedView.listContainer = listContainerView
        view.addSubview(listContainerView)
        
        oldMobileVC.buttonHandle = { status, name in
            self.userInfoVC.status = status
            self.newPwdVC.name = name
            self.segmentedView.selectItemAt(index: 1)
        }
        
        userInfoVC.buttonHandle = { identy, bankcardNo in
            self.newPwdVC.identy = identy
            self.newPwdVC.bankcardNo = bankcardNo
            self.segmentedView.selectItemAt(index: 2)
        }
        
        newMobileVC.buttonHandle = { codeToken, newMobile in
            self.newPwdVC.codeToken = codeToken
            self.newPwdVC.newMobile = newMobile
            self.segmentedView.selectItemAt(index: 3)
        }
        childVcs = [oldMobileVC, userInfoVC, newMobileVC, newPwdVC]
    }
    override func customNavBackAction() {
        let dataSource = segmentedView.dataSource as? JXSegmentedTitleDataSource
        if currentIndex == 0 {
            navigationController?.popViewController(animated: true)
        } else {
            // 调用 selectItemAt 方法来切换到下一个页面
            segmentedView.selectItemAt(index: currentIndex - 1)
        }
    }
}

extension KAChangeMobileVC: JXSegmentedViewDelegate {
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        if let dotDataSource = segmentedDataSource as? JXSegmentedDotDataSource {
            //先更新数据源的数据
            dotDataSource.dotStates[index] = false
            //再调用reloadItem(at: index)
            segmentedView.reloadItem(at: index)
        }
        currentIndex = index
        navigationController?.interactivePopGestureRecognizer?.isEnabled = (segmentedView.selectedIndex == 0)
    }
}

extension KAChangeMobileVC: JXSegmentedListContainerViewDataSource {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        if let titleDataSource = segmentedView.dataSource as? JXSegmentedBaseDataSource {
            return titleDataSource.dataSource.count
        }
        return 0
    }
    
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        let value = childVcs[index]
        return value
    }
}


